/*
Esta libreria requiere que se incluya las librerias Common.js, DragDrop.js y ContextMenu.js
*/

var ColumnManager_Instances = [];

function ColumnManager(id, gridID)
{
    this.id = id;
    this.gridID = gridID;
    this.titles = [];
        
    this.containerAll = null;
    this.containerVisibles = null;
    this.containerOrders = null;

    this.containerVisiblesCopy = null;
    this.containerOrdersCopy = null;
    
    this.setContainers = ColumnManager_setContainers;
    this.buildContextMenus = ColumnManager_buildContextMenus;
    
    this.toXml = ColumnManager_toXml;
    this.renderVisibles = ColumnManager_renderVisibles;
    this.renderOrderBy = ColumnManager_renderOrderBy;
    this.addXmlNode = ColumnManager_addXmlNode;
    
    this.refresh = ColumnManager_Refresh;
    this.getNewItem = ColumnManager_getNewItem;
    this.getAbDirection = ColumnManager_GetAbDirection; 
    
    ColumnManager_Instances[id] = this;
}

function ColumnManager_setContainers(contAll, contVisibles, contOrders, trash)
{
    this.containerAll = document.getElementById(contAll);
    this.containerVisibles = document.getElementById(contVisibles);
    this.containerOrders = document.getElementById(contOrders);
    
    CreateDragSet(this.containerAll, this.containerVisibles, this.containerOrders);

    if(trash)
    {
        var objTrash = document.getElementById(trash);        
        SetTrash(objTrash);
    }
}

function ColumnManager_Refresh(doc)
{
    var columns = doc.getElementsByTagName('Column');

    // Clean Column Containers
    this.containerVisibles.innerHTML = "";
    this.containerOrders.innerHTML = "";

    // Refill Visible and OrderBy Containers
    for (var i=0; i<columns.length; i++)
    {
        var field = columns[i].getAttribute("Field");        
        var title = this.titles[field];
        var visible = columns[i].getAttribute("Visible"); 
        var orderBy = columns[i].getAttribute("Order");
    
        if(visible == "True") {
            var newItem = this.getNewItem(field, title);
            newItem.innerHTML = title;
                
            this.containerVisibles.appendChild(newItem);
        }
        
        if(orderBy != "None") {
            var newItem = this.getNewItem(field, title);
            newItem.setAttribute("direction", orderBy);
            Common.setEventHandler(newItem, "onclick", "ColumnManager_swapDirection(this, event)");
            Common.setEventHandler(newItem, "onmouseup", "ColumnManager_contextualMenu(this, event)");

            newItem.innerHTML = title + " (" + this.getAbDirection(orderBy) + ")";

            this.containerOrders.appendChild(newItem);
        }
    }      
    
    SetDragContainer(this.containerVisibles, "target", 0);
    SetDragContainer(this.containerOrders, "target", 0);                  
}

function ColumnManager_getNewItem(field, title)
{
    var newItem = document.createElement("div");
    newItem.className = "dragBox";
    
    newItem.setAttribute("id", field)
    newItem.setAttribute("title", title)
    newItem.setAttribute("parent", "Orders")
    newItem.setAttribute("idManager", this.id)
    newItem.setAttribute("overClass", "overDragBox")
    newItem.setAttribute("dragClass", this.cssClass + "_dragingBox ")
    newItem.setAttribute("afterMoveFunction", "ColumnManager_afterMove")

    return newItem;
}

function ColumnManager_GetAbDirection(orderBy)
{
    switch (orderBy)
    {
        case "Descending":
            return "Desc";
        case "Ascending":
            return "Asc";
        default:
            return "";
    }
}


// -----------------------------------------------------------------------------------
// Operaciones con las columnas 
// -----------------------------------------------------------------------------------

function ColumnManager_afterMove(item)
{
    var oldParent = item.getAttribute("parent");
    var newParent = item.parentNode.getAttribute("nickname");
    
    // el elemento cambio de container
    if(oldParent != newParent)
    {
        if(newParent == "Orders")
        {
            var title = item.getAttribute("title");
            var direction = "Ascending"; // direccion default
            var strDir = ColumnManager_getAbDirection(direction);            
            
            item.innerHTML = title + " (" + strDir + ")";
            item.setAttribute("direction", direction);            
            Common.setEventHandler(item, "onclick", "ColumnManager_swapDirection(this)");
            Common.setEventHandler(item, "onmouseup", "ColumnManager_contextualMenu(this, event)");
        }
        
        if(newParent == "Visibles")
        {
            Common.setEventHandler(item, "onmouseup", "ColumnManager_contextualMenu(this, event)");
        }
                
        item.setAttribute("parent", newParent);
    }
}

function ColumnManager_contextualMenu(item, ev)
{
    var id = item.getAttribute("idManager");
    var cm = ColumnManager_Instances[id];
    
	ev = ev || window.event;
	
	if(ev.button==2)
	{
	    var parent = item.getAttribute("parent");
	    
	    if( (parent == "Visibles") && (cm.visiblesMenu) )
	    {
            cm.visiblesMenu.show(item, ev);
	    }
	    
	    if(parent == "Orders")
	    {
            cm.ordersMenu.show(item, ev);
	    }	    
	}
}

function ColumnManager_swapDirection(item)
{
    var direction = item.getAttribute("direction");
    var title = item.getAttribute("title");
    
    if(direction == "Ascending") {
        ColumnManager_changeDirection(item, "Descending")
    }
    else {
        ColumnManager_changeDirection(item, "Ascending")
    }
}

function ColumnManager_changeDirection(item, direction)
{
    var title = item.getAttribute("title");
    var strDir = ColumnManager_getAbDirection(direction);
        
    item.setAttribute("direction", direction);
    item.innerHTML = title + " (" + strDir + ")";
}

function ColumnManager_getAbDirection(direction)
{
    if(direction == "Ascending")
        return "Asc";
    else
        return "Desc";
}
       
       
// -------------------------------------------------------------------------------------------------------
// Context Menus
// -------------------------------------------------------------------------------------------------------

function ColumnManager_buildContextMenus(contextMenuClass)
{
    // Menu order by
    this.ordersMenu = new ContextMenu("order");

    this.ordersMenu.addItem("Descending", "Descending");
    this.ordersMenu.addItem("Ascending", "Ascending");
    
    this.ordersMenu.cssClass = contextMenuClass;
    this.ordersMenu.width = "100px";
    this.ordersMenu.acceptFunction = "ColumnManager_changeDirection";
}


// -----------------------------------------------------------------------------------------------
//  Apply Changes
// -----------------------------------------------------------------------------------------------

function ColumnManager_toXml()
{
    var xmlColumns = document.createElement("Columns");
    
    this.renderVisibles(xmlColumns);    
    this.renderOrderBy(xmlColumns);
    
	return xmlColumns;
}

function ColumnManager_renderVisibles(xmlColumns)
{
    var contVisibles = this.containerVisibles;
    
    var field;
    var groupOp;
    var node;
    	
    // Getting Visible Columns	
    for(var i=0; i<contVisibles.childNodes.length; i++) 
    {
		// Firefox puts in lots of #text nodes...skip these
		if(contVisibles.childNodes[i].nodeName != "#text")
		{
    		field = contVisibles.childNodes[i].getAttribute('id');
		    this.addXmlNode(xmlColumns, field, "true", "None")
		}
	}
}

function ColumnManager_renderOrderBy(xmlColumns)
{
    var contOrder = this.containerOrders;
    
    var field;
    var dir;
    var node;
    	
    // Getting Visible Columns	
    for(var i=0; i<contOrder.childNodes.length; i++) 
    {
		// Firefox puts in lots of #text nodes...skip these
		if(contOrder.childNodes[i].nodeName != "#text")
		{
    		field = contOrder.childNodes[i].getAttribute('id');
    		dir = contOrder.childNodes[i].getAttribute('direction');
		    
		    this.addXmlNode(xmlColumns, field, "false", dir)
		}
	}
}

function ColumnManager_addXmlNode(xmlDocument, field, visible, order)
{
    var node = document.createElement("Column");
    
    node.setAttribute("Field", field);
    node.setAttribute("Visible", visible);
    node.setAttribute("Order", order);		    
    
    xmlDocument.appendChild(node);
}


// -----------------------------------------------------------------------------------
//  AJAX
// -----------------------------------------------------------------------------------

function ColumnManager_GetXml(id) 
{
    var columnManager = ColumnManager_Instances[id];
    
    var columns = columnManager.toXml();
    var xmlColumns = "<Columns>" + columns.innerHTML + "</Columns>";
    
    return xmlColumns;
}
